package com.quanxi.qxaimain.common.datasource.mybatiesplus;



import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import com.quanxi.qxaimain.common.datasource.datasourceAOP.enums.DataSourceEnum;
import com.quanxi.qxaimain.common.datasource.datasourceAOP.multiple.MultipleDataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Properties;



/**
 * @Author lyx
 * @create 2022/8/31 11:30
 */
@Configuration
@MapperScan("com.quanxi.qxaimain.dao")
public class MyBatiesPlusConfiguration {

    @Bean(name="db1")
    @ConfigurationProperties(prefix="spring.datasource.druid.db1" )
    public DataSource db1(){

        return  DruidDataSourceBuilder.create().build();}

    @Bean(name = "db2")
    @ConfigurationProperties(prefix ="spring.datasource.druid.db2" )
    public DataSource db2() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "db3")
    @ConfigurationProperties(prefix ="spring.datasource.druid.db3" )
    public DataSource db3() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "db4")
    @ConfigurationProperties(prefix ="spring.datasource.druid.db4" )
    public DataSource db4() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "db5")
    @ConfigurationProperties(prefix ="spring.datasource.druid.db5" )
    public DataSource db5() {


        return DruidDataSourceBuilder.create().build();
    }

    /**
     * 动态数据源配置
     * @return
     */
    @Bean
    @Primary
    public DataSource multipleDataSource(@Qualifier("db1") DataSource db1,
                                         @Qualifier("db2") DataSource db2,
                                         @Qualifier("db3") DataSource db3,
                                         @Qualifier("db4") DataSource db4,
                                         @Qualifier("db5") DataSource db5){
        MultipleDataSource multipleDataSource = new MultipleDataSource();
        HashMap<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceEnum.DB1.getDBvalue(), db1);
        targetDataSources.put(DataSourceEnum.DB2.getDBvalue(), db2);
        targetDataSources.put(DataSourceEnum.DB3.getDBvalue(), db3);
        targetDataSources.put(DataSourceEnum.DB4.getDBvalue(), db4);
        targetDataSources.put(DataSourceEnum.DB5.getDBvalue(), db5);
        //添加数据源
        multipleDataSource.setTargetDataSources(targetDataSources);
        //设置默认数据源
        multipleDataSource.setDefaultTargetDataSource(db5);

        return multipleDataSource;
    }



    /**
     * SQL执行效率插件
     */
    @Bean
    @Profile({"dev","qa"})// 设置 dev t 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(1000);
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }




    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(multipleDataSource(db1(),db2(),db3(),db4(),db5()));
        //mapper 下两级目录包含一级  第三级不再匹配
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/**/**/*.xml"));

        MybatisConfiguration configuration = new MybatisConfiguration();
        //configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);

        configuration.setCallSettersOnNulls(true);
        configuration.setCallSettersOnNulls(true);//如果查询语句中某些字段值是null的，让这个字段可以返回
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);


        return sqlSessionFactory.getObject();
    }

    /*@Bean
    public GlobalConfiguration globalConfiguration() {
        GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector());
        conf.setLogicDeleteValue("-1");
        conf.setLogicNotDeleteValue("1");
        conf.setIdType(0);
        //conf.setMetaObjectHandler(new MyMetaObjectHandler());
        conf.setDbColumnUnderline(true);
        conf.setRefresh(true);
        return conf;
    }*/




}